
function delAttachment(obj, aid) {
    var titlname = $(obj).attr("title");
    if (confirm("确定要删除:" + titlname + " 吗？")) {
        $.post('/ajax-api/dellinkageone.html', {aid: aid}, function(result) {
            if (result.code == 200) {
                alert(result.message);
                $(obj).parent().parent().remove();
            } else {
                alert(result.message);
            }
        }, 'json');
    }
}
//获取附件信息
function editAttachment(id) {
    $.post("/ajax-api/linkageone.html", {id: id}, function(attachment) {
        var form = document.getElementById("attachForm");
        if (attachment.id) {
            form.id.value = attachment.id;
            form.value.value = attachment.value;
            form.order.value = attachment.order;
            form.name.value = attachment.name;
            form.status.value = attachment.status;
            $("#myModal").modal('toggle');
        } else {
            alert(attachment);
        }
    }, 'json');

}
$(document).ready(function() {
    $(".btn-weight").on('click', function() {
        $(".modal-title").html("修改权重");
        $(".modal-body").show();
        $(".modal-body-pic").hide();
        var form = $("#attachForm");
        $(form).children('.modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>');
        var h = '<button type="button" onClick="saveWeight()" class="btn btn-primary">保存</button>';
        $(h).appendTo($(form).children('.modal-footer'));
        $(form).find("input[name='id']").val($(this).attr('valt'));
        $(form).find("input[name='weight']").val($(this).attr('tval'));
        $("#myModal").modal('toggle');
    });
    $(".btn-online").on('click', function() {
        var status = $(this).attr('tval');
        if (status == '1') {
            $(".modal-title").html("确定下线");
        } else {
            $(".modal-title").html("确定上线");
        }

        $(".modal-body").hide();
        $(".modal-body-pic").hide();
        var form = $("#attachForm");
        $(form).children('.modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>');
        var h = '<button type="button" onClick="saveStatus()" class="btn btn-primary">确定</button>';
        $(h).appendTo($(form).children('.modal-footer'));
        $(form).find("input[name='id']").val($(this).attr('valt'));
        $(form).find("input[name='weight']").val(status);
        $("#myModal").modal('toggle');
    });
    $(".btn-picshow").on('click', function() {
        $(".modal-body").hide();
        $(".modal-body-pic").show();
        var form = $("#attachForm");
        $(form).children('.modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>');
        $("#showPic").attr("src",$(this).attr('tval'));
        $("#myModal").modal('toggle');
    });
});
function saveWeight() {
    var form = $("#attachForm");
    var id = $(form).find("input[name='id']").val();
    var setvalue = $(form).find("input[name='weight']").val();
    if ((/^(\+|-)?\d+$/.test(setvalue)) && setvalue >= 0) {
        //判断是设置的数据是否为0或者整数
        $.post("/advert/change.html", {id: id, setvalue: setvalue, type: 'Weight'}, function(data) {
            if (data.code == '200') {
                alert('更改成功');
                window.location.reload();
            } else {
                alert(data.message);
            }
        }, 'json');
    } else {
        alert("权重需要输入整数！");
    }

}
function saveStatus() {
    var form = $("#attachForm");
    var id = $(form).find("input[name='id']").val();
    var setvalue = $(form).find("input[name='weight']").val();
    if ((/^(\+|-)?\d+$/.test(setvalue)) && setvalue >= 0) {
        //判断是设置的数据是否为0或者整数
        if (setvalue == '1') {
            setvalue = '2';
        } else {
            setvalue = '1';
        }
        $.post("/advert/change.html", {id: id, setvalue: setvalue, type: 'Status'}, function(data) {
            if (data.code == '200') {
                alert('处理成功');
                window.location.reload();
            } else {
                alert(data.message);
            }
        }, 'json');
    } else {
        alert("错误的状态值");
    }

}
//提交表单修改附件信息
function ajaxEditAttachment() {
    var form = document.getElementById("attachForm");
    var id = form.id.value;
    var value = form.value.value;
    var order = form.order.value;
    var name = form.name.value;
    var pos_ename = form.pos_ename.value;
    var status = form.status.value;
    $.post('/ajax-api/edlinkageone.html', {id: id, value: value, order: order, name: name, status: status, pos_ename: pos_ename}, function(result) {
        if (result.code == 200) {
            $('#myModal').modal('hide');
            location.reload();
        }
        alert(result.message);
    }, 'json');
}